﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.FtpClient;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace LibraryManagement
{
    public static class clsMethods
    {
        public static XDocument xDoc;
        public static void CreateXML(List<vwBook> listvwBooks)
        {
            try
            {
                #region Creating XML Document
                var context = new LibManagementEntities();
                xDoc = new XDocument(
                    new XDeclaration("1.0", "UTF-8", "yes"),
                    new XElement("Root"));
                xDoc.Descendants("Root").FirstOrDefault().Add(new XElement("Books"));
                xDoc.Descendants("Root").FirstOrDefault().Add(new XElement("Members"));
                foreach (vwBook book in listvwBooks)
                {
                    xDoc.Descendants("Books").FirstOrDefault().Add(
                            new XElement("Book",
                                new XAttribute("Id", book.Id),
                                new XAttribute("Title", book.Title),
                                new XAttribute("Author", book.Author1),
                                new XAttribute("Publisher", book.Publisher),
                                new XAttribute("Copies", book.Copies),
                                new XAttribute("Location", book.LocationName)));
                }
                foreach (MemberDetail member in context.MemberDetails.ToList())
                {
                    xDoc.Descendants("Members").FirstOrDefault().Add(
                            new XElement("Member",
                                new XAttribute("Id", member.MemberEnrollmentNo),
                                new XAttribute("Pin", member.PinCode)));
                }

                DirectoryInfo di;
                if (!Directory.Exists(Application.StartupPath + "\\XML Data"))
                {
                    di = Directory.CreateDirectory(Application.StartupPath + "\\XML Data");
                    //di.Attributes = FileAttributes.Directory | FileAttributes.Hidden;
                }
                xDoc.Save(Application.StartupPath + "\\XML Data\\BookDetails.xml");
                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public async static void UploadToFtp(String FilePath, String FileName)
        {
            try
            {
                FtpWebRequest request2 = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.my3gb.com/" + FileName);
                request.Method = WebRequestMethods.Ftp.UploadFile;
                request.Credentials = new NetworkCredential("mztestingid", "murtaza123");
                StreamReader sourceStream = new StreamReader(FilePath);
                byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
                sourceStream.Close();
                request.ContentLength = fileContents.Length;
                request.ConnectionGroupName = "MyConnection";
                Stream requestStream = request.GetRequestStream();
                requestStream.Write(fileContents, 0, fileContents.Length);

                WebResponse response = await request.GetResponseAsync();
                FtpWebResponse resp2 = (FtpWebResponse)response;
                MessageBox.Show("Upload File Complete, status {0}", resp2.StatusDescription);
                resp2.Close();
            }
            catch (Exception ex)
            {
            }
        }

        public static void UploadToFtpUsingFtpClient(String FilePath, String FileName)
        {
            try
            {
                FtpClient client = new FtpClient();
                client.Host = "ftp.my3gb.com";
                client.Credentials = new NetworkCredential("mztestingid", "murtaza123");
                client.Connect();

                using (var fileStream = File.OpenRead(FilePath))
                using (var ftpStream = client.OpenWrite(FileName))
                {
                    var buffer = new byte[8 * 1024];
                    int count;
                    while ((count = fileStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        ftpStream.Write(buffer, 0, count);
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }


        internal static byte[] ConvertImageToByteArray(System.Drawing.Image imageToConvert, System.Drawing.Imaging.ImageFormat formatOfImage)
        {
            if (imageToConvert != null)
            {
                byte[] Ret;
                try
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        imageToConvert.Save(ms, formatOfImage);
                        Ret = ms.ToArray();
                    }
                }
                catch (Exception) { throw; }
                return Ret;
            }
            else
                return null;
        }

        internal static void LoadImageFromBytes(byte[] image, dynamic obj, PictureBox pcb)
        {
            try
            {
                if (image != null)
                {
                    FileStream fs = new FileStream("image.jpg", FileMode.Create);
                    fs.Write(image, 0, obj.Image.Length);
                    fs.Close(); fs = null;
                    pcb.Image = Image.FromFile("image.jpg");
                    pcb.Refresh();
                }
            }
            catch (Exception)
            {
            }
        }


    }
}
